package com.lanjiu.im.netty.server.common;





import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;



/**
 * @author zha_gtao
 * @data
 * AES对称加密解密
 */
public class AesSecret {

    private static final Logger logger = LoggerFactory.getLogger(AesSecret.class);

    /**
     *加密
     * 1.构造秘钥生成器
     * 2.根据ecnodeRules规则初始化秘钥生成器
     * 3.产生秘钥
     * 4.创建和初始化密码器
     * 5.内容加密
     * 6.返回字符串
     */
    public static String AESEncode(String encodeRules,String content){
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128,new SecureRandom(encodeRules.getBytes()));
            SecretKey original_key = keygen.generateKey();
            byte[] raw = original_key.getEncoded();
            SecretKey key = new SecretKeySpec(raw, "AES");

            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE,key);
            byte[] bytes_encode = content.getBytes("utf-8");
            byte[] bytes_AES = cipher.doFinal(bytes_encode);
            String AES_encoder = new String(new BASE64Encoder().encode(bytes_AES));
            return AES_encoder;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return content;
    }

    /**
     * 解密
     * 1.同加密1-4步
     * 2.将加密后的字符串反成byte[]数组
     * 3.将加密内容解密
     */
    public static String AESDncoder(String encoderRules,String content){
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128,new SecureRandom(encoderRules.getBytes()));
            SecretKey original_key = keygen.generateKey();
            byte[] raw = original_key.getEncoded();
            SecretKeySpec key = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE,key);
            byte[] bytes_content = new BASE64Decoder().decodeBuffer(content);

            byte[] bytes_decode = cipher.doFinal(bytes_content);
            String AES_decode = new String(bytes_decode, "utf-8");
            return AES_decode;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        logger.error("AES解密异常内容:"+content);
        return content;
    }

    public static void main(String[] args) {
        AesSecret aesSecret = new AesSecret();
        String encodeRules = "AES对称加密";
        String content = "alskgjioVN反馈那你";
        String befor = AesSecret.AESEncode(encodeRules, content);
        System.out.println("加密后："+befor);
        String after = AesSecret.AESDncoder(encodeRules,befor);
        System.out.println("解密后："+after);


    }

}
